#!/usr/local/bin/python3

from os import popen 
from os import system
import re,time

PATH = "~/Downloads/"
comandDisItems = "ls " + PATH
comandMvItem = "mv " + PATH 
ToPATH = " ~/Downloads/GZ-BZ2/"
list = popen(comandDisItems)

def check_tar_gz_bz2(file_name):
    if " " in file_name:
        file_name = file_name.replace(" ","\ ")
    if check_tool_re(file_name) :
        return file_name
    else :
        return False

def check_tool_re(text):
    pattern = re.compile(r'\.tar\.gz$|\.tar\.bz2$')
    try:
        re.search(pattern,text).group()
        return True
    except AttributeError:
        return False


def mv_gz_bz2_file(file_name):
    COMA = comandMvItem + file_name[:-1] + ToPATH
    #print(file_name[:-1])  #test
    system (COMA)
    return True

def main_gz_bz2(files):
    for File in files:
        if check_tar_gz_bz2(File) != False:
            mv_gz_bz2_file(File)
        else:
            pass
    print("tar.gz --------- clear ok ")
    print("tar.bz2 --------- clear ok ")
if __name__ == "__main__":
    main_gz_bz2(list)
    print(time.asctime())
